package com.leyou.item.controller;

import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.IBrandService;
import com.leyou.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BrandController {

    @Autowired
    private IBrandService brandService;

    /**
     * 根据bids批量查询品牌
     * @param ids
     * @return
     */
    @GetMapping("/brand/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }

    /**
     * 根据bid查询品牌
     * @param bid
     * @return
     */
    @GetMapping("/brand/{bid}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("bid") Long bid) {
        return ResponseEntity.ok(brandService.queryBrandById(bid));
    }

    /**
     * 根据分类查询品牌的集合
     * @param id
     * @return
     */
    @GetMapping("/brand/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandsByCategory(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandsByCategory(id));
    }

    /**
     * 新增品牌
     * @param brandDTO
     * @param cids
     * @return
     */
    @PostMapping("brand")
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.saveBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 品牌分页查询
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("/brand/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,  //当前页
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,   //页面大小
            @RequestParam(value = "sortBy",required = false)String sortBy,   //是否排序
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc,    //是否正序
            @RequestParam(value = "key",required = false)String key     //搜索关键字
    ) {
        PageResult<BrandDTO> result = brandService.queryBrandByPage(page, rows, sortBy, desc, key);
        return ResponseEntity.ok(result);
    }
}
